package br.edu.ufam.controleAcesso

class Usuario {
    
    static auditable = true

    static mapping = {
        grupo lazy:false
        id generator:'uuid'
    }

    String id
    String nome
    String login
    String senha
    String confirmaSenha
    String mail
    Grupo grupo
    
    Date dateCreated
    Date lastUpdated

    static transients = ['confirmaSenha']
    //static belongsTo = [Grupo]

    static constraints = {
        nome(nullable: false, blank: false, maxSize:250)
        login(nullable: false, blank: false, maxSize:11, unique:true)
        senha(nullable: false, blank: false)
        confirmaSenha(nullable:false, blank:false,maxSize:32, password:true, validator: {val, obj -> obj.properties['senha'] == val}) //verifica se Senha é igual a ConfSenha
        mail(nullable:true, blank:true, email:true)
        grupo(nullable: false, blank: false)
        //grupo(nullable: true, blank: true)
    }

    String toString(){
        "${nome}"
    }

    public def getAplicacoes(){
        Set listaAp = [] as Set             
        Aplicacao.executeQuery("select g.aplicacao from GrupoAplicacao g where g.grupo.id = ? and g.aplicacao.dtinicio <= sysdate and g.aplicacao.dtfim >= sysdate and g.aplicacao.ativo = true", [this.grupo.id]).each{
            listaAp << it
        }
        Aplicacao.executeQuery("select u.aplicacao from UsuarioAplicacao u where u.usuario.id = ? and u.aplicacao.dtinicio <= sysdate and u.aplicacao.dtfim >= sysdate and u.aplicacao.ativo = true", [this.id]).each{
            listaAp << it
        }
        return listaAp.sort{ it.descricao }
    }

}
